﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Web.Authentication;
using App.Domain.Contracts;
using Microsoft.Practices.Unity;
using System.Web.Security;
using MileageStats.Web.Authentication;

namespace App.Web.Controllers
{
    public class AuthController : Controller {
        private readonly IFormsAuthentication formsAuthentication;
        private readonly IUserServices userServices;

        [InjectionConstructor]
        public AuthController( IFormsAuthentication formsAuthentication,
                              IUserServices userServices) {
            this.formsAuthentication = formsAuthentication;
            this.userServices = userServices;
        }

        //
        // GET: /Auth/
        public ActionResult SignIn(string UserName, string PWD)
        {
            //TODO 验证
            var user = this.userServices.GetOrCreateUser(UserName,PWD);
            if (user != null)
            {
                this.formsAuthentication.SetAuthCookie(this.HttpContext,
                                                               UserAuthenticationTicketBuilder.CreateAuthenticationTicket(
                                                                   user));

                return this.RedirectToAction("List","Position");
            }
            return this.RedirectToAction("SignIn");
        }

        public ActionResult SignInWithProvider(string UserName,string PWD) {

            //TODO 验证
            //var user = this.userServices.GetOrCreateUser(response.ClaimedIdentifier);
            //this.formsAuthentication.SetAuthCookie(this.HttpContext,
            //                                               UserAuthenticationTicketBuilder.CreateAuthenticationTicket(
            //                                                   user));
            return this.RedirectToAction("SignIn");
        }

        public ActionResult SignInResponse(string UserName, string PWD)
        {
            //var response = this.relyingParty.GetResponse();

            //switch (response.Status)
            //{
            //    case AuthenticationStatus.Authenticated:
            //        var user = this.userServices.GetOrCreateUser(response.ClaimedIdentifier);
            //        this.formsAuthentication.SetAuthCookie(this.HttpContext,
            //                                               UserAuthenticationTicketBuilder.CreateAuthenticationTicket(
            //                                                   user));

            //        return this.RedirectToRoute("Dashboard");

            //    case AuthenticationStatus.Canceled:
            //        this.TempData["Message"] = "Cancelled Authentication";
            //        return this.RedirectToAction("SignIn");

            //    case AuthenticationStatus.Failed:
            //        this.TempData["Message"] = response.Exception.Message;
            //        return this.RedirectToAction("SignIn");

            //    default:
            //        this.TempData["Message"] = Resources.AuthController_SignInResponse_Unable_to_authenticate;
            //        return this.RedirectToAction("SignIn");
            //}

            return null;
        }

        public ActionResult SignOut() {
            this.formsAuthentication.Signout();
            return this.RedirectToAction("Index", "Home");
        }
    }
}
